/**
 * 系统平台
 * moonvip_admin
 * com.tz.web.good
 * GoodController.java
 * 创建人:小胡
 * 时间：2016年12月07日 00:40:36
 *
 */
package com.tz.web.good;

import com.tz.auth.AuthPassport;
import com.tz.bean.Good;
import com.tz.bean.GoodChild;
import com.tz.bean.TzParams;
import com.tz.commonModel.GoodChildModel;
import com.tz.service.good.IGoodService;
import com.tz.service.goodcatogory.IGoodcatogoryService;
import com.tz.service.goodchild.IGoodChildService;
import com.tz.util.StringHelper;
import com.tz.util.TmStringUtils;
import com.tz.util.TzConstant;
import com.tz.util.qiniuUtils.QiniuParamter;
import com.tz.util.qiniuUtils.QiniuUtil;
import org.apache.struts2.json.JSONException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

/**
 * 平台商品管理web
 * GoodController
 * 创建人:小胡
 * 时间：2016年12月07日 00:40:36
 * @version 1.0.0
 * 
 */
@Controller
@RequestMapping("/admin/shop/good")
public class GoodController {

	@Resource
	private IGoodService goodService;
	@Resource
	private IGoodChildService goodChildService;
	@Resource
	private IGoodcatogoryService goodcatogoryService;

	private final static String filePath = TzConstant.PATH_GOOD_PATH;
	private final static String key_suff = "shop/goodcatogory/";


	/*列表查询*/
	@AuthPassport
	@RequestMapping("/list")
	public String list(TzParams params) {
		return "shop/good/list";
	}

	/*模板页面*/
	@RequestMapping("/template")
	public String template(TzParams params, Model model) {
		params.setDeleteCode("001");
		model.addAttribute("datas", goodService.findGoods(params));
		model.addAttribute("itemCount", goodService.count(params));
		return "shop/good/template";
	}


	@AuthPassport
	@RequestMapping("/add")
	public String add(TzParams params, Model model) {
		List<HashMap<String, Object>> firstcatogorys = goodcatogoryService.getFirstCatogary();
		model.addAttribute("firstcatogorys", firstcatogorys);
		return "shop/good/add";
	}

	@AuthPassport
	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public String edit(@PathVariable("id") Integer goodId, Model model) {
		List<HashMap<String, Object>> firstcatogorys = goodcatogoryService.getFirstCatogary();
		model.addAttribute("firstcatogorys", firstcatogorys);   //得到全部分类
		HashMap<String, Object> good = goodService.getGood(goodId);
		//得到所有的详情图

		model.addAttribute("good", good);
		return "shop/good/edit";
	}

	@AuthPassport
	@ResponseBody
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public String delete(TzParams params) {
		goodService.delete(params);
		return "success";
	}

	/**
	 * 进入修改图片轮播图页面
	 */
	@AuthPassport
	@RequestMapping(value = "/editPicture/{goodId}")
	public String editPicture(@PathVariable Integer goodId, Model model){
		//获得所有的轮播图
		model.addAttribute("allPictures", goodService.getAllPictures(goodId));
		model.addAttribute("goodId", goodId);
		return "shop/good/editPicture";
	}


	/**
	 * 进入修改商品规格页面
	 */
	@AuthPassport
	@RequestMapping(value = "/editSpec/{goodId}")
	public String editSpec(@PathVariable Integer goodId, Model model){
//		//组装商品规格的一个集合  List<Map<String,Object>>
//		//<Map<String,Object>>中包含的key有 name 取值可以为规格名称, 集合 各个规格的具体
//		//判断依据，集合的个数size，如果不为空，则放入集合中
//		List<String> colors =  goodService.getColorByGoodId(goodId);
//		List<String> weights =  goodService.getWeightByGoodId(goodId);
//		List<String> materias =  goodService.getMaterialByGoodId(goodId);
//		List<String> mainstores =  goodService.getMainstoreByGoodId(goodId);
//		List<String> handSizes =  goodService.getHandSizeByGoodId(goodId);
//		List<String> necklacelens =  goodService.getNecklacelenByGoodId(goodId);
//		List<String> braceletlens =  goodService.getBraceletlenByGoodId(goodId);
//		List<Double> prices =  goodService.getPriceByGoodId(goodId);
//		List<Integer> stockNums =  goodService.getstockNumByGoodId(goodId);
//		List<Map<String,Object>> dataList = new ArrayList<>();
//
//		if(colors.size()>0){
//			Map map1 = new HashMap();
//			map1.put("name","颜色");
//			map1.put("attr","color");
//			map1.put("results",colors);
//			dataList.add(map1);
//		}
//		if(weights.size()>0){
//			Map map2 = new HashMap();
//			map2.put("name","重量");
//			map2.put("attr","weight");
//			map2.put("results",weights);
//			dataList.add(map2);
//		}
//		if(materias!=null && !materias.isEmpty()){
//			Map map3 = new HashMap();
//			map3.put("name","材质");
//			map3.put("attr","material");
//			map3.put("results",materias);
//			dataList.add(map3);
//		}
//		if(mainstores.size()>0){
//			Map map4 = new HashMap();
//			map4.put("name","主石规格");
//			map4.put("attr","mainStoreSpec");
//			map4.put("results",mainstores);
//			dataList.add(map4);
//		}
//		if(handSizes.size()>0){
//			Map map5 = new HashMap();
//			map5.put("name","戒指手寸");
//			map5.put("attr","handSize");
//			map5.put("results",handSizes);
//			dataList.add(map5);
//		}
//		if(necklacelens.size()>0){
//			Map map6 = new HashMap();
//			map6.put("name","项链长度");
//			map6.put("attr","necklace_len");
//			map6.put("results",necklacelens);
//			dataList.add(map6);
//		}
//		if(braceletlens.size()>0){
//			Map map7 = new HashMap();
//			map7.put("name","手链长度");
//			map7.put("attr","bracelet_len");
//			map7.put("results",braceletlens);
//			dataList.add(map7);
//		}
//     	model.addAttribute("goodId", goodId);
//		model.addAttribute("dataList", dataList);
//		return  "shop/good/editgui";

		model.addAttribute("goodId", goodId);
		TzParams params = new TzParams();
		params.setDeleteCode("001");
		params.setGoodId(goodId);
		List<GoodChild> goodchilds = goodChildService.findGoodChilds(params);
		model.addAttribute("goodchilds", goodchilds);
		return  "shop/good/editSpec";
	}

	@ResponseBody
	@RequestMapping("/getSecondCatogory")
	public List<HashMap<String, Object>> getSecondCatogory(TzParams params) {
		List<HashMap<String, Object>> secondcatogorys = goodcatogoryService.getSecondCatogary(params);
		return secondcatogorys;
	}

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public String save(Good good, Integer[] stocknumber, String[] color, String[] imgurl) throws IOException {
		goodService.saveGoodAndChild(good, stocknumber, color, imgurl);
		return "redirect:list";
	}

	@ResponseBody
	@RequestMapping(value = "/saveGood", method = RequestMethod.POST)
	public String saveGood(Good good, String[] detailimgurl, Integer[] catogoryId, String goodchild) throws IOException {
		goodService.saveGood(good, detailimgurl, catogoryId, goodchild);
		System.out.println("1111");
		return "ok";
	}



//	@RequestMapping(value = "/edit/{id}" ,method = RequestMethod.GET)
//	public String edit(@PathVariable("id") Integer goodId ,Model model){
//		HashMap<String ,Object> good = goodService.getGood(goodId);
//		TzParams params = new TzParams();
//		params.setGoodId(goodId);
//		params.setCanPage(false);
//		List<GoodChild> goodChildren = goodChildService.findGoodChilds(params);
//		model.addAttribute("good",good);
//		model.addAttribute("goodChildren",goodChildren);
//		List<HashMap<String , Object>> firstcatogorys = goodcatogoryService.getFirstCatogary();
//		model.addAttribute("firstcatogorys" , firstcatogorys);
//		params.setPid((Integer) good.get("categoryPid"));
//		List<HashMap<String , Object>> secondcatogorys = goodcatogoryService.getSecondCatogary(params);
//		model.addAttribute("secondcatogorys" , secondcatogorys);
//		return "shop/good/edit";
//	}

	@ResponseBody
	@RequestMapping(value = "/update",method =  RequestMethod.POST)
	public String update(Good good) throws IOException {
		goodService.update(good);
		return "success";
	}


	@RequestMapping(value = "/updateGoodAndChild")
	public String updateGoodAndChild(Good good, GoodChildModel goodChildModel) throws IOException {
		goodService.updateGoodAndChild(good, goodChildModel);
		return "redirect:list";
	}



	@ResponseBody
	@RequestMapping("/uploadQiniu")
	public String uploadQiniu(HttpServletRequest request, Integer categoryId) throws IOException {
		//上传到本地目录
		String path = StringHelper.upload(request, filePath);
		//拼接上传到七牛的路径
		String key = key_suff + categoryId + path.substring(path.lastIndexOf("/"));
		//上传到七牛
		QiniuUtil.upload(path, key);
		//获取上传七牛的图片外链
		String imgurl = QiniuParamter.URL + key;
		return imgurl;
	}

	@RequestMapping("/getAllcatogories")
	public String getAllcatogories(Integer opid) {
		return "shop/good/catogories";
	}

	@ResponseBody
	@RequestMapping(value = "/findCatogoryByGoodId", method = RequestMethod.POST)
	public Integer[] findCatogoryByGoodId(HttpServletRequest requset) throws JSONException {
		String goodId = requset.getParameter("goodId");
		if (TmStringUtils.isNotEmpty(goodId)) {
			Integer[] catogoyIds = goodService.findCatogoryByGoodId(Integer.parseInt(goodId));
			return catogoyIds;
		} else {
			return null;
		}
	}

	/**
	 * 描述: 重新保存商品和分类中间表
	 * 包名： com.tz.web.role
	 * 类名：RoleController
	 * 方法名: saveRolePermission
	 * 参数类型: [request]
	 * 创建人：hufanglei
	 * 时间：2016/3/19   8:50
	 * qq：690328661
	 *
	 * @return java.lang.String
	 */
	@ResponseBody
	@RequestMapping(value = "/saveGoodAndCatogory", method = RequestMethod.POST)
	public String saveGoodAndCatogory(HttpServletRequest request) {
		//获取所有的分类
		String catogoriesStirng = request.getParameter("catogories");
		//获取分配的角色
		String goodId = request.getParameter("goodId");
		goodService.saveGoodAndCatogory(goodId, catogoriesStirng);
		return "success";
	}



    /**
     * 修改详情页轮播图
     */
    @ResponseBody
    @RequestMapping(value = "/updateDetailImg",method = RequestMethod.POST)
    public String updateDetailImg(@RequestParam(value = "delIds[]") int[] delIds, @RequestParam(value = "addImgs[]") String[] addImgs,Integer goodId) throws IOException {
		return  goodService.updateDetailImg(delIds, addImgs, goodId);
    }

	/**
	 * 修改商品基本信息
	 * @param good
	 * @return
	 * @throws IOException
	 */
    @ResponseBody
	@RequestMapping(value = "/updateGood",method = RequestMethod.POST)
	public int  updateGood(Good good) throws IOException {
		int n = goodService.updateGood(good);
		return n;
	}



	@ResponseBody
	@RequestMapping(value = "updateSpec",method = RequestMethod.POST)
	public int  updateSpec(GoodChild goodChild){
		int n = goodService.updateSpec(goodChild);
		return  n;
	}

}
